package com.xcfw.dorm.controller.dormassistant;

import com.google.gson.JsonObject;
import com.xcfw.dorm.controller.BaseController;
import com.xcfw.dorm.entity.ExamineStaff;
import com.xcfw.dorm.entity.NumEnum;
import com.xcfw.dorm.entity.ReturnJson;
import com.xcfw.dorm.exception.ServiceException;
import com.xcfw.dorm.exception.ValidationException;
import com.xcfw.dorm.mapper.ExamineStaffMapper;
import com.xcfw.dorm.util.DormUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: Dorm
 * @description: 分组
 * @author: Bruse Queen
 * @create: 2018-05-08 11:43
 **/
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/dormAssistant")
public class ExamineStaffController extends BaseController {
    @Autowired
    private ExamineStaffMapper examineStaffMapper;

    @RequestMapping(value = "/addExamineStaff", method = RequestMethod.POST)
    public ReturnJson addExamineStaff(HttpServletRequest request) {
        try {
            JsonObject jsonObject = getJsonObject(request);
            JsonObject examine = jsonObject.get("examine").getAsJsonObject();
            JsonObject dutyroom = jsonObject.get("dutyroom").getAsJsonObject();


//            Map<String, String> mapExamine = new LinkedHashMap<>();
//            mapExamine.put("date", new Date().toString());
//            mapExamine.put("1", "孙驰鸿,吴梓铭,郑宏杨,谢宗霖,周翔,胡睿祺");
//            mapExamine.put("2", "罗绍瑜,李希彬,余忠海,徐弘,闫志宝,李宇航");
//            mapExamine.put("3", "杨涛,谢凯,郑庭宇,李南波,邱宇锋,官益民");
//            mapExamine.put("4", "文东林,柏金,王港生,林蜀州,赵琳,王硕");
//            mapExamine.put("5", "黄骞,张旭,聂思源,杨鑫,王俊玺,左肖");
//
//            Map<String, String> mapDuty = new HashMap<>();
//            mapDuty.put("date", new Date().toString());
//            mapDuty.put("duty", "朱仕明,梁同,兰赐宇,白崇霖");


            ExamineStaff examineStaff = new ExamineStaff();
            examineStaff.setExamine(examine.toString());
            examineStaff.setDutyroom(dutyroom.toString());

            if (1 != examineStaffMapper.insertSelective(examineStaff)) {
                throw new ServiceException("新增查寝组合失败!");
            }

            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "查寝组合新增成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("ExamineStaffController---addExamineStaff--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/deleteExamineStaff", method = RequestMethod.POST)
    public ReturnJson deleteExamineStaff(@RequestBody ExamineStaff examineStaff) {
        try {
            if (1 != examineStaffMapper.deleteByPrimaryKey(examineStaff.getId())) {
                throw new ServiceException("删除失败！");
            }
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "查寝组合删除成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("ExamineStaffController---deleteExamineStaff--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/updateExamineStaff", method = RequestMethod.POST)
    public ReturnJson updateExamineStaff(@RequestBody ExamineStaff examineStaff) {
        try {
            if (1 != examineStaffMapper.updateByPrimaryKeySelective(examineStaff)) {
                throw new ServiceException("修改失败!");
            }
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "查寝组合修改成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("ExamineStaffController---updateExamineStaff--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/selectExamineStaff", method = RequestMethod.POST)
    public ReturnJson selectExamineStaff(@RequestBody ExamineStaff examineStaff) {
        try {
            if ("".equals(examineStaff.getId())) {
                examineStaff.setId(null);
            }
            List<ExamineStaff> list = examineStaffMapper.selectActionSql(DormUtil.setNull(examineStaff.getId()));
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "查寝组合查询成功！", list);
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("ExamineStaffController---selectExamineStaff--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }


}
